// pages/question/index.js
import { IndexModel } from "../../models/index.js";
const indexModel = new IndexModel();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        topicList:null,
        titleMsg:"",
        topicCount:0,
        currentIndex:0,
        page:1,
        sourceType:0,
        typeId:0,
        toastShow:false,
        userAnswer:null,
        pageTitle:'',
        modeNum:0
    },

    nextQuestion(event){
        let userAnswer=event.detail.isAnswer;
        let clickPage=event.detail.clickPage;
        let isAddOne=0;
        if(clickPage==undefined){
            clickPage=this.data.page;
            isAddOne=1;
        }
        if(!userAnswer){
            userAnswer=null;
        }
        if(event.detail.indexNum>=this.data.topicCount){
            let msgTitle="";
            if(this.data.sourceType==0||this.data.sourceType==1){
                msgTitle="恭喜您!本小节刷题已完成";
            }else if(this.data.sourceType==2){
                msgTitle="恭喜您!题库全部刷新完成";
            }else if(this.data.sourceType==7){
                msgTitle="恭喜您!今日练习完成";
            }
            if(msgTitle!=""){
                wx.showToast({
                    title: msgTitle,
                    icon: 'none',
                    duration: 2000
                })
            }
        }else{
            if(event.detail.topicIndex>=this.data.topicList.length-1||clickPage!=this.data.page){
                let topIndex=event.detail.topicIndex+1;
                if(isAddOne==1){
                    clickPage=clickPage+1;
                    topIndex=0;
                }
                this.loadData(this.data.sourceType,this.data.typeId,clickPage,0,topIndex);
            }else{
                this.setData({
                    currentIndex:event.detail.topicIndex+1
                })
                if(this.data.topicList[this.data.currentIndex].user_answer_record){
                    userAnswer=this.data.topicList[this.data.currentIndex].user_answer_record;
                }
            }
            
            this.setData({
                userAnswer:userAnswer
            });
        }
       
    },
    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        let sourceType=options.sourceType;
        let title="";
        let modeNum=0;
        if(sourceType==0){
            title="专项练习";
        }else if(sourceType==1){
            title="历年真题";
        }else if(sourceType==2){
            title="刷题练习";
        }else if(sourceType==3){
            title="收藏夹";
        }else if(sourceType==4){
            title="错题本";
        }else if(sourceType==5){
            title="我的笔记";
        }else if(sourceType==8){
            title="考试结果";
            modeNum=1;
        }
        this.setData({
            pageTitle:title,
            modeNum:modeNum
        });
        let typeId=options.typeId;
        this.loadData(sourceType,typeId,this.data.page,1,0);
    },
    async loadData(sourceType,typeId,page,isLoad,index) {
        const data=await indexModel.getQuestionList(sourceType,typeId,page);
        const newpage=(data.topics.length>0?data.page:this.data.page);
        let currentIndex=index;
        if(isLoad==2){
            //从上次答题记录过来的
            currentIndex=data.progress%10-1;
        }
        this.setData({
            topicList:data.topics,
            titleMsg:data.titleMsg,
            topicCount:data.topicCount,
            sourceType:sourceType,
            typeId:typeId,
            page:newpage,
            currentIndex:currentIndex
          });
          if(isLoad==1){
              if(data.topicCount==0){
                  //暂无数据；
                  wx.showToast({
                    title: "暂无数据",
                    icon: 'none',
                    duration: 2000,
                    success:function(){
                        setTimeout(function () { 
                            wx.switchTab({
                                url: `/pages/index/index`,
                              });
                         }, 2000) 
                
                        
                    }
                  })
              }
            var that = this;
              //页面加载；判断是不是原来有做题记录
              if(data.progress>0){
                wx.showModal({
                    title: '',
                    content: '上次做到第'+data.progress+'题,是否继续',
                    confirmText:'继续',
                    cancelText:(sourceType==7?"重新抽取":"重新答题"),
                    success (res) {
                      if (res.confirm) {
                          //加载上次答题所在的那一页数据
                          if(data.progressPage==page){
                                //上次答的题所在页面和本次请求一样；不用请求了;直接到目标数据
                                let curIndex=data.progress%10;
                                that.setData({
                                    currentIndex:(curIndex?(curIndex-1):(data.progress-1))
                                });
                          }else{
                            that.loadData(sourceType,typeId,data.progressPage,2,0);
                          }
                        
                      } else if (res.cancel) {
                        //重新答题了；请求接口将之前记录清除
                         indexModel.resetDoProgress(sourceType,typeId);
                         if(sourceType==7){
                             //重洗获取题目
                             that.loadData(sourceType,typeId,1,1,0);
                         }
                      }
                    }
                  })
                  
              }
          }
          let userAnswer=this.data.topicList[this.data.currentIndex].user_answer_record;
          this.setData({
             userAnswer:userAnswer
          });
      },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})